package com.hr.controller;

import com.hr.dto.JsonResp;
import com.hr.model.Company;
import com.hr.model.Position;
import com.hr.model.User;
import com.hr.model.Wanted;
import com.hr.service.*;
import com.hr.util.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/rec")
public class RecController
{
    @Autowired
    ArchiveService archiveService;
    @Autowired
    WantedService wantedService;
    @Autowired
    CompanyService companyService;
    @Autowired
    DepartmentService departmentService;
    @Autowired
    PositionService positionService;
    @Autowired
    UserService userService;


    @RequestMapping(value = "/mar/get", method = RequestMethod.GET)
    public ResponseEntity<JsonResp> getWanteds()
    {
        return new ResponseEntity<>(JsonResp.ok("wanteds", wantedService.getAllWanteds()), HttpStatus.OK);
    }



    @RequestMapping(value = "/my/get", method = RequestMethod.GET)
    public ResponseEntity<JsonResp> getWantedByUser()
    {
        int uid=((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId();
        List<Wanted> wanteds=wantedService.getWantedsByUId();
        for(Wanted wanted: wanteds)
        {
            wanted.setStatus(wantedService.getWantedStatusByU_W_Id(uid,wanted.getId()));
        }
        return new ResponseEntity<>(JsonResp.ok("wanteds",wanteds), HttpStatus.OK);
    }



    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    public ResponseEntity<JsonResp> modifyWanted(@RequestParam(required = false) Integer uid, @RequestParam int wid, @RequestParam String status, @RequestBody(required = false) Position position)
    {
        switch (status)
        {
            case "待审核":
            {
                //检查是否启用
                if(!wantedService.getWantedByWId(wid).isEnable())
                    return new ResponseEntity<>(JsonResp.badRequest("该招聘以停止"), HttpStatus.BAD_REQUEST);
                //检查是否已申请
                if((wantedService.getUWByU_W_Id(AuthUtil.getCurUser().getId(), wid))!=null)
                    return new ResponseEntity<>(JsonResp.badRequest("已申请过该职位"), HttpStatus.BAD_REQUEST);
                //检查是否有档案
                if(archiveService.getArchiveByUId(AuthUtil.getCurUser().getId())==null)
                    return new ResponseEntity<>(JsonResp.badRequest("尚未简历档案，请建立后再投递"), HttpStatus.BAD_REQUEST);

                //新增
                wantedService.insertUW(AuthUtil.getCurUser().getId(),wid,status);
                return new ResponseEntity<>(JsonResp.ok("投递成功"), HttpStatus.OK);
            }
            case "已通过":
            case "未通过":
            {
                //检查原先状态
                if(!wantedService.getUWByU_W_Id(uid, wid).getStatus().equals("待审核"))
                    return new ResponseEntity<>(JsonResp.badRequest("该操作不合法"), HttpStatus.BAD_REQUEST);
                //检查管理权限
                if(!wantedService.isHrOfWanted(wid))
                    return new ResponseEntity<>(JsonResp.badRequest("权限不足"), HttpStatus.BAD_REQUEST);
                //修改
                wantedService.updateUW(uid,wid,status);
                return new ResponseEntity<>(JsonResp.ok("修改成功"), HttpStatus.OK);
            }
            case "已接受":
            case "已拒绝":
            {
                //检查原先状态
                if(!wantedService.getUWByU_W_Id(uid, wid).getStatus().equals("已通过"))
                    return new ResponseEntity<>(JsonResp.badRequest("该操作不合法"), HttpStatus.BAD_REQUEST);
                //检查是否属于当前用户
                for(Wanted wanted:wantedService.getWantedsByUId())
                {
                    if(wanted.getId()==wid)
                    {
                        //修改
                        wantedService.updateUW(uid,wid,status);
                        return new ResponseEntity<>(JsonResp.ok("修改成功"), HttpStatus.OK);
                    }
                }
                return new ResponseEntity<>(JsonResp.badRequest("该流程属于其他用户或不存在"), HttpStatus.BAD_REQUEST);
                //修改
            }
            case "已入职":
            {
                //验证职位信息
                if(position==null)
                    return new ResponseEntity<>(JsonResp.badRequest("职位错误"), HttpStatus.BAD_REQUEST);
                //设置职位所属公司,
                int cid=wantedService.getWantedByWId(wid).getCompany().getId();
                position.setCompany(new Company());
                position.getCompany().setId(cid);
                //验证部门属于公司
                if(!departmentService.ifDepartmentBelong2Company(position.getDepartment().getId(),cid))
                    return new ResponseEntity<>(JsonResp.badRequest("部门不属于该公司"), HttpStatus.BAD_REQUEST);
                //检查原先状态
                if(!wantedService.getUWByU_W_Id(uid, wid).getStatus().equals("已接受"))
                    return new ResponseEntity<>(JsonResp.badRequest("该操作不合法"), HttpStatus.BAD_REQUEST);
                //检查管理权限
                if(!wantedService.isHrOfWanted(wid))
                    return new ResponseEntity<>(JsonResp.badRequest("权限不足"), HttpStatus.BAD_REQUEST);
                //设置职位
                positionService.insertPosition(userService.getUserByUId(uid).getArchive().getId(),position);
                //修改
                wantedService.updateUW(uid,wid,status);
                return new ResponseEntity<>(JsonResp.ok("修改成功"), HttpStatus.OK);
            }
            default:
                return new ResponseEntity<>(JsonResp.badRequest("未知状态"), HttpStatus.BAD_REQUEST);
        }
    }

}
